package com.esd.chyl.controller;

import java.util.UUID;

import com.esd.chyl.service.StandardService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.esd.chyl.model.Standard;
import com.esd.chyl.base.BaseService;

/**
 * 
 * @author snailzhang
 *
 */
@Controller
public class LoginController {
	private static final Logger logger = LogManager.getLogger(LoginController.class);

	@Autowired
	private StandardService userService;
	
	
	//@Autowired
	//private UserService userService;

//	/**
//	 * 用户名已存在
//	 */
//	@Value("${MSG_USER_EXIST}")
//	private String MSG_USER_EXIST;
//
//	/**
//	 * 用户不存在
//	 */
//	@Value("${MSG_USER_NOT_EXIST}")
//	private String MSG_USER_NOT_EXIST;

	/**
	 * 退出页面
	 * 
	 * @return
	 */
//	@RequestMapping(value = "/quit", method = RequestMethod.GET)
//	public ModelAndView quitGet(HttpSession session) {
//		logger.debug("清空Session里的登录信息");
//		session.removeAttribute(Constants.USER_ID);
//		logger.debug("退出，跳转到登录页");
//		return new ModelAndView("login");
//	}

//	/**
//	 * 登录页面
//	 * 
//	 * @return
//	 */
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String loginGet(Model model) {
		logger.info("跳转到登录页");
		model.addAttribute("user", new Standard());
		// return new ModelAndView("login");
		return "auth_login";
	}
	
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String loginPost(@ModelAttribute(value="user") Standard user) {
		logger.info("提交登录");
		logger.info(user.getField0());
		logger.info(user.getField1());
		user.setId(UUID.randomUUID().toString().replace("-", ""));
		userService.insert(user);
		// return new ModelAndView("login");
		return "redirect:index";
	}
//
//	/**
//	 * 检查用户名是否重复
//	 * 
//	 * @param userName
//	 * @return
//	 */
//	@RequestMapping(value = "/checkUserName", method = RequestMethod.POST)
//	@ResponseBody
//	public Map<String, Object> checkUserName(HttpServletRequest request) {
//		String userName = request.getParameter("username");
//		logger.info("检测用户名");
//		logger.debug("username:{}", userName);
//		Map<String, Object> map = new HashMap<String, Object>();
//		User use = userService.selectByName(userName);
//		if (use != null) {
//			map.put("status", true);
//			map.put(Constants.MESSAGE, MSG_USER_EXIST);
//		} else {
//			map.put("status", false);
//			map.put(Constants.MESSAGE, MSG_USER_NOT_EXIST);
//		}
//		return map;
//	}
//
//	/**
//	 * 登录提交处理
//	 * 
//	 * @param request
//	 * @param redirectAttributes
//	 * @param session
//	 * @return
//	 */
//	@RequestMapping(value = "/login", method = RequestMethod.POST)
//	public ModelAndView loginPost(HttpServletRequest request, RedirectAttributes redirectAttributes,
//			HttpSession session) {
//		String userName = request.getParameter("username");
//		String passWord = request.getParameter("password");
//		//String checkCode = request.getParameter("checkCode");
//		
//		//String sessionCheckCode = session.getAttribute("checkCode").toString();
//
////		logger.info("userName:{},passWord:{},checkCode:{}", userName, passWord, checkCode);
//		logger.info("userName:{},passWord:{}", userName, passWord);
//
////		if (checkCode.equals(sessionCheckCode) == false) {
////			redirectAttributes.addFlashAttribute("username", userName);
////			redirectAttributes.addFlashAttribute("password", passWord);
////			redirectAttributes.addFlashAttribute("message", "验证码错误");
////			logger.info("验证码错误");
////			return new ModelAndView("redirect:/login");
////		}
//
//		User user = userService.selectByName(userName);
//		if (user != null && user.getUsername().equals(userName)) {
//			UsernameAndPasswordMd5 md5 = new UsernameAndPasswordMd5();
//			String pwd = md5.getMd5(userName, passWord);
//			//logger.info("pwd:", pwd);
//			if (pwd.equals(user.getPassword())) {
//				session.setAttribute(Constants.USER_ID, user.getUserId());
//				session.setAttribute(Constants.USER_NAME, user.getUsername());
//				session.setAttribute(Constants.USER_TYPE, user.getUserType());
//				logger.info("登录成功");
//				user.setUpdateTime(new Date());
//				userService.updateByPrimaryKey(user);
////		 		return new ModelAndView("redirect:/security/index");
//				session.setAttribute(Constants.USER_ID, user.getUserId());
//				session.setAttribute(Constants.USER_NAME, user.getUsername());
//				return new ModelAndView("redirect:/security/order");
//
//			} else {
//				redirectAttributes.addFlashAttribute("username", userName);
//				redirectAttributes.addFlashAttribute("password", passWord);
//				redirectAttributes.addFlashAttribute("message", "用户名密码错误");
//				logger.info("用户名密码错误");
//				return new ModelAndView("redirect:/login");
//			}
//		}
//		redirectAttributes.addFlashAttribute("username", userName);
//		redirectAttributes.addFlashAttribute("password", passWord);
//		redirectAttributes.addFlashAttribute("message", "用户名密码错误");
//		logger.info("用户名密码错误");
//		return new ModelAndView("redirect:/login");
//	}
}
